define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/house/house/index' + location.search,
                    add_url: 'xiluhouse/house/house/add',
                    edit_url: 'xiluhouse/house/house/edit',
                    del_url: 'xiluhouse/house/house/del',
                    multi_url: 'xiluhouse/house/house/multi',

                    housetype_url: 'xiluhouse/house/housetype/index',
                    housedeveloper_url:'xiluhouse/house/housedeveloper/index',
                    housetag_url: 'xiluhouse/house/housetag/index',
                    house_url:'xiluhouse/house/house/index',
                    dragsort_url:'',
                    table: 'house',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='house_type_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.housetype_url).data("primaryKey", "id").data("field", "name");
                $("input[name='house_developer_id']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.housedeveloper_url).data("primaryKey", "id").data("field", "name");
                $("input[name='house_tag_ids']", form).addClass("selectpage").data("source", $.fn.bootstrapTable.defaults.extend.housetag_url).data("primaryKey", "id").data("field", "name").data("params", function (obj) {
                    return {custom: {classify: 1}};
                });
            })
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable:true},
                        {field: 'name', title: __('楼盘基本信息'),operate:'LIKE',formatter: function (value, row, index) {
                            var html='';
                            html+='<div>'+ row.name +'('+row.alias_name+')</div>';
                            html+='<div>'+__('Price')+':'+row.price+'</div>';
                            html+='<div>'+__('Min_total_price')+':'+row.min_total_price+'</div>';
                            html+='<div>'+__('Max_total_price')+':'+row.max_total_price+'</div>';
                            return html;
                        }},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image,operate:false},
                        {field: 'alias_name', title: __('Alias_name'),operate:'LIKE',visible:false},
                        {field: 'sale_state', title: __('Sale_state'), searchList: {"1":__('Sale_state 1'),"2":__('Sale_state 2'),"3":__('Sale_state 3')}, formatter: Table.api.formatter.normal},
                        {field: 'is_low_price_protect', title: __('Is_low_price_protect'), searchList: {"1":__('Is_low_price_protect 1'),"2":__('Is_low_price_protect 2')}, formatter: Table.api.formatter.normal,visible:false},
                        {field: 'price', title: __('Price'),sortable:true, operate:'BETWEEN',visible:false},
                        {field: 'min_total_price', title: __('Min_total_price'),sortable:true, operate:'BETWEEN',visible:false},
                        {field: 'max_total_price', title: __('Max_total_price'),sortable:true, operate:'BETWEEN',visible:false},
                        
                        {field: 'build_image', title: __('Build_image'), events: Table.api.events.image, formatter: Table.api.formatter.image,visible:false,operate:false},
                        {field: 'prov', title: __('Prov'),operate:'LIKE',formatter: function (value, row, index) {
                            var html='';
                            html+='<div>'+ row.prov+' '+row.city+' '+row.region +'</div>';
                            html+='<div>'+row.street+' '+row.addr+'</div>';
                            return html;
                        }},
                        {field: 'city', title: __('City'),operate:'LIKE',visible:false},
                        {field: 'region', title: __('Region'),operate:'LIKE',visible:false},
                        {field: 'street', title: __('Street'),operate:'LIKE',visible:false},
                        {field: 'addr', title: __('Addr'),operate:'LIKE',visible:false},
                        {field: 'lat', title: __('Lat'),visible:false},
                        {field: 'lng', title: __('Lng'),visible:false},
                        {field: 'subway_distance', title: __('Subway_distance'), operate:'BETWEEN',visible:false},
                        {field: 'subway_distance_text', title: __('Subway_distance_text'),operate:'LIKE',visible:false},
                        {field: 'house_type_id', title: __('House_type_id'),formatter: function (value, row, index) {
                            if(row.house_type_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.housetype_url+ "?id=" + row.house_type_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息"> '+row.housetype.name+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        }
                        },
                        {field: 'house_developer_id', title: __('House_developer_id'),formatter: function (value, row, index) {
                            if(row.house_developer_id>0){
                                var html='';
                                var url =$.fn.bootstrapTable.defaults.extend.housedeveloper_url+ "?id=" + row.house_developer_id;
                                html+='<a  href="' + url + '" class="bg-info btn-dialog" title="查看关联信息">'+row.housedeveloper.name+' </a>';
                                return html;
                            }else{
                                return '-';
                            }
                        },visible:false},
                        { field: 'house_tag_ids', title: __('House_tag_ids'),operate:'FIND_IN_SET',formatter: function (value, row, index) {
                            if (row.house_tag_ids_text_arr) {
                                return Table.api.formatter.flag(row.house_tag_ids_text_arr, row, index);
                            }else{
                                return '-';
                            }
                        },visible:false},
                        
                        {field: 'min_room_num', title: __('Min_room_num'), operate:'BETWEEN',visible:false},
                        {field: 'max_room_num', title: __('Max_room_num'), operate:'BETWEEN',visible:false},
                        {field: 'min_space', title: __('Min_space'), operate:'BETWEEN',visible:false},
                        {field: 'max_space', title: __('Max_space'), operate:'BETWEEN',visible:false},
                        {field: 'opentime', title: __('Opentime'), operate:'RANGE', addclass:'datetimerange',visible:false, formatter: Table.api.formatter.datetime},
                        {field: 'deliverytime', title: __('Deliverytime'), operate:'RANGE',visible:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'nearby_bus_num', title: __('Nearby_bus_num'), operate:'BETWEEN',visible:false},
                        {field: 'nearby_subway_num', title: __('Nearby_subway_num'), operate:'BETWEEN',visible:false},
                        {field: 'nearby_shop_num', title: __('Nearby_shop_num'), operate:'BETWEEN',visible:false},
                        {field: 'nearby_hospital_num', title: __('Nearby_hospital_num'), operate:'BETWEEN',visible:false},
                        {field: 'view_num', title: __('View_num'), operate:'BETWEEN',visible:false},
                        {field: 'sale_num', title: __('Sale_num'), operate:'BETWEEN',visible:false},
                        {field: 'collect_num', title: __('Collect_num'), operate:'BETWEEN',visible:false},
                        {field: 'receive_num', title: __('Receive_num'), operate:'BETWEEN',visible:false},
                        {field: 'cosult_tel', title: __('Cosult_tel'), operate:'LIKE',visible:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,visible:false},
                        {field: 'viewtime', title: __('Viewtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,visible:false},
                        {field: 'saletime', title: __('Saletime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,visible:false},
                        {field: 'collecttime', title: __('Collecttime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,visible:false},
                        {field: 'recievetime', title: __('Recievetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,visible:false},
                        {field: 'weigh', title: __('Weigh'),sortable:true},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                        buttons: [], formatter: Table.api.formatter.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var buttons = that.buttons;
                                buttons[0]={
                                    name: 'trend', 
                                    text: '动态管理', 
                                    title: '动态管理',
                                    icon: 'fa fa-flash', 
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: 'xiluhouse/house/housetrends/index?house_id='+row.id
                                };
                                
                                that.buttons = buttons;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiluhouse/house/house/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiluhouse/house/house/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiluhouse/house/house/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                $("form[role=form]").on('validation', function(e, current){
                    var form = this;
                    // form 中是否所有字段都验证通过
                    if(!current.isValid){
                        var parents = $(current.element).parents(".tab-pane");
                        if(!parents.hasClass("active")){
                            var index = parents.index();
                            $(".nav-tabs li").eq(index).find("a[data-toggle='tab']").trigger('click');
                            //$(".nav-tabs li").eq(index).addClass('active').siblings().removeClass('active');
                        }
                    }
                    return false;
                });

                var validatoroptions = {
                    invalid: function (form, errors) {
                        $.each(errors, function (i, j) {
                            Layer.msg(j);
                        });
                    },
                    //ignore: ':hidden'
                };
                $('form[role=form]').data("validator-options", validatoroptions);
                $(document).on("fa.event.appendfieldlist", '.images_fieldlist', function(){
                    Form.events.plupload(this);
                    Form.events.faselect(this);
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});